<script>
	import {registerDevice} from "@/api/common.js"
	export default {
		onLaunch: function() {
			uni.removeStorageSync('canLogin')
			this.getDeviceInfo()
		},
		onShow: function() {
			console.log('App Show');
		},
		onHide: function() {
			console.log('App Hide');
		},
		methods:{
			//注册设备
			registerDevice(){
				const params = {
					version: "V10001",
					channel:'IOS',
					deviceId: uni.getStorageSync("deviceId")
				} 
				registerDevice(params).then(res => {
					if(res.code == "0000"){
						const newVersionCode = res.data?.version || "V10001";
						uni.setStorageSync("newVersionCode",newVersionCode)
						plus.runtime.getProperty(plus.runtime.appid,(wgtinfo) => {
							const versionCode = Number(wgtinfo.versionCode);
							if(newVersionCode == "V00002"){//有更新
								uni.showModal({
									title: 'version update',
									content: `Detected a new version`,
									confirmText: 'update now',
									success: (result) => {
										if (result.confirm) {
											const appleId = this.COFIG?.appleId;
											plus.runtime.launchApplication({
												action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
											}, function(e) {
												console.log('Open system default browser failed: ' + e.message);
											});
										}
									}
								})
							}
						})
					}
				})
			},
			
			// 获取设备信息
			getDeviceInfo(){
				try{
					plus.device.getInfo({
						success:(e) => {
							let deviceId = e?.uuid?.replace(/-/g, "");
							uni.setStorageSync('deviceId',deviceId);
							this.registerDevice()
						},
						fail:(e) => {
							console.log('deviceInfo failed: '+JSON.stringify(e));
							this.registerDevice()
						}
					});
				}
				catch (e) {
					this.registerDevice()
				}
			}
		}
	}
</script>

<style lang="scss">
	/************************************************************
  ** 请将全局样式拷贝到项目的全局 CSS 文件或者当前页面的顶部 **
  ** 否则页面将无法正常显示                                  **
  ************************************************************/
	@import '@/common/css/index.scss';
</style>